/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package babycalendar;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
import java.util.Calendar;
import javax.swing.Timer;

/**
 *
 * @author kipe
 */
public class systemTrayBaby extends Thread {

    private TrayIcon trayIcon;
    private String numberOfWeeks="32";
    private SystemTray tray;
    private boolean allDone = false;
    
    public systemTrayBaby(String inString) {
        numberOfWeeks = inString;
    }
        
    @Override
    public void run() {
        if (SystemTray.isSupported()) {

            tray = SystemTray.getSystemTray();

            MouseListener mouseListener = new MouseListener() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getButton()==e.BUTTON1){
                        BabyCalendarApp.getApplication().getView().getFrame().setVisible(true);
                        if (isAllDone()) {
                            tray.remove(trayIcon);
                        }
                    }
                }

                @Override
                public void mouseEntered(MouseEvent e) {
//                    System.out.println("Tray Icon - Mouse entered!");
                }

                @Override
                public void mouseExited(MouseEvent e) {
//                    System.out.println("Tray Icon - Mouse exited!");
                }

                @Override
                public void mousePressed(MouseEvent e) {
//                    System.out.println("Tray Icon - Mouse pressed!");
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    System.out.println("Tray Icon - Mouse released!");
                }
            };

            ActionListener exitListener = new ActionListener() {

                public void actionPerformed(ActionEvent e) {
//                    System.out.println("Exiting...");
                    System.exit(0);
                }
            };
            ActionListener optionsListener = new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    BabyCalendarApp.getApplication().getView().getFrame().setVisible(true);
                    if (isAllDone()) {
                        tray.remove(trayIcon);
                    }
                }
            }; 

            PopupMenu popup = new PopupMenu();
            MenuItem defaultItem = new MenuItem("Exit");
            defaultItem.addActionListener(exitListener);
            MenuItem optionsItem = new MenuItem("Options");
            optionsItem.addActionListener(optionsListener);
            popup.add(optionsItem);
            popup.add(defaultItem);
            DecimalFormat df = new DecimalFormat("##.#");
                    
            trayImage = stringToImage(numberOfWeeks);
            trayIcon = new TrayIcon(trayImage, "Ez a terhesség "+ numberOfWeeks+". hete.", popup);

            ActionListener actionListener = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    trayIcon.displayMessage("Action Event",
                            "An Action Event Has Been Peformed!",
                            TrayIcon.MessageType.INFO);
                }
            };
            
            trayIcon.setImageAutoSize(true);
            trayIcon.addActionListener(actionListener);
            trayIcon.addMouseListener(mouseListener);

            //    Depending on which Mustang build you have, you may need to uncomment
            //    out the following code to check for an AWTException when you add 
            //    an image to the system tray.

            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
//                System.err.println("TrayIcon could not be added.");
            }

        } else {
//            System.err.println("System tray is currently not supported.");
        }
        allDone = true;
    }
    
    public BufferedImage stringToImage(String input) {
           int w=24, h = 24;
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        //        Graphics2D g = bi.createGraphics();
        Graphics g = bi.getGraphics();
        Font f = new Font("Tahoma", Font.BOLD, 16);
        //        g.setBackground(Color.white);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, w, h);
        g.setColor(Color.BLACK);
        g.setFont(f);
        int stringWidth = g.getFontMetrics().stringWidth(input);
        if (stringWidth > w) {
            f = new Font("Tahoma", Font.BOLD, (int) (16 * 0.95 * w / stringWidth));
            g.setColor(Color.BLACK);
            g.setFont(f);
            stringWidth = g.getFontMetrics().stringWidth(input);
        }
        g.drawString(input, (int) ((w - stringWidth) / 2), (int) (h * 0.7));
        g.dispose();
        return bi;
    }
   private BufferedImage trayImage;

    public String getNumberOfWeeks() {
        return numberOfWeeks;
    }

    public void setNumberOfWeeks(String numberOfWeeks) {
        this.numberOfWeeks = numberOfWeeks;
    }

    public boolean isAllDone() {
        return allDone;
    }

    public void setAllDone(boolean allDone) {
        this.allDone = allDone;
    }

}
